
#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
#include <windows.h>

int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	PROCESS_INFORMATION pi;

	HKEY hKey; DWORD dwDisposition;

	if ( ERROR_SUCCESS == RegCreateKeyEx(
		  HKEY_LOCAL_MACHINE
		, _T("SOFTWARE\\Microsoft\\.NETCompactFramework")
		, 0
		, NULL
		, 0
		, 0
		, NULL
		, &hKey
		, &dwDisposition ) )
	{
		RegSetValueEx( hKey, _T("CommandLine"), 0, REG_SZ, (LPBYTE)lpCmdLine, (_tcslen(lpCmdLine)+1)*sizeof(wchar_t) );

		RegCloseKey( hKey );
	}

	// run _cgacutil.exe; wait for it to finish; get it's exit code; return the exit code
	DWORD dwExitCode = -1;
	if (0 != CreateProcess( _T("_cgacutil.exe"), lpCmdLine, NULL, NULL, false, 0, 0, 0, 0, &pi ) )
	{
		if ( WAIT_OBJECT_0 == WaitForSingleObject( pi.hProcess, INFINITE ) )
			GetExitCodeProcess( pi.hProcess, &dwExitCode );
	}

	if ( ERROR_SUCCESS == RegOpenKeyEx(
		  HKEY_LOCAL_MACHINE
		, _T("SOFTWARE\\Microsoft\\.NETCompactFramework")
		, 0
		, 0
		, &hKey ))
	{
		RegDeleteKey( hKey, _T("CommandLine") );
		RegCloseKey ( hKey );
	}

	if (pi.hProcess) CloseHandle( pi.hProcess );
	if (pi.hThread ) CloseHandle( pi.hThread  );

	return dwExitCode;
}
